package nakedb;

import java.awt.Graphics2D;
import java.awt.Shape;

import prefuse.render.AbstractShapeRenderer;
import prefuse.render.LabelRenderer;
import prefuse.render.ShapeRenderer;
import prefuse.visual.VisualItem;

/**
 * Renderer of a Label and Shape at the same time.
 *
 */
public class LabelShapeRenderer extends AbstractShapeRenderer {

  private ShapeRenderer m_shapeRenderer;
  private LabelRenderer m_labelRenderer;
  
  private boolean shapeOn = false;
    
  public LabelShapeRenderer() {
    m_shapeRenderer = new ShapeRenderer();
    m_labelRenderer = new LabelRenderer();
    m_labelRenderer.setRenderType(AbstractShapeRenderer.RENDER_TYPE_NONE);
  }

  public LabelShapeRenderer(String textField, int size) {
    m_shapeRenderer = new ShapeRenderer(size);
    m_shapeRenderer.setRenderType(RENDER_TYPE_NONE);
    
    m_labelRenderer = new LabelRenderer(textField);
    m_labelRenderer.setRoundedCorner(8, 8);
    //m_labelRenderer.setRenderType(AbstractShapeRenderer.RENDER_TYPE_DRAW_AND_FILL);
  }
  
  public void activateShape(boolean shapeOn) {
    if (this.shapeOn != shapeOn) {
      if (shapeOn) {
        m_shapeRenderer.setRenderType(RENDER_TYPE_DRAW_AND_FILL);
        m_labelRenderer.setRenderType(AbstractShapeRenderer.RENDER_TYPE_NONE);
      } else {
        m_shapeRenderer.setRenderType(RENDER_TYPE_NONE);
        m_labelRenderer.setRenderType(AbstractShapeRenderer.RENDER_TYPE_DRAW_AND_FILL);
      }
      this.shapeOn = shapeOn;
    }
  }
  
  public void setTextField(String text) {
    m_labelRenderer.setTextField(text);
  }
  
  public boolean isShapeActivated() {
    return shapeOn;
  }
  
  public void setRoundedCorner(int n1, int n2) {
    m_labelRenderer.setRoundedCorner(n1, n2);
  }

  @Override
  public void render(Graphics2D g, VisualItem item) {
    if (shapeOn) {
      m_shapeRenderer.render(g, item);
    }
    m_labelRenderer.render(g, item);
  }

  @Override
  // /XXX getRawShape should return the "raw", not transformed shape!
  // Actually, it just returns the (already transformed) ShapeRenderers shape.
  protected Shape getRawShape(VisualItem item) {
    if (shapeOn) {
      return m_shapeRenderer.getShape(item);
    } else {
      return m_labelRenderer.getShape(item);
    }
  }

}